package cn.tedu.store.user.webapi.service.impl;

import cn.tedu.store.commons.ex.ServiceException;
import cn.tedu.store.commons.pojo.user.dto.AddressAddNewDTO;
import cn.tedu.store.commons.pojo.user.dto.AddressUpdateDTO;
import cn.tedu.store.commons.pojo.user.entity.Address;
import cn.tedu.store.commons.pojo.user.vo.AddressListItemVO;
import cn.tedu.store.commons.pojo.user.vo.AddressStandardVO;
import cn.tedu.store.commons.web.JsonPage;
import cn.tedu.store.commons.web.ServiceCode;
import cn.tedu.store.user.service.IAddressService;
import cn.tedu.store.user.webapi.mapper.AddressMapper;
import cn.tedu.store.user.webapi.security.LoginPrincipal;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class AddressServiceImpl implements IAddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public void addNew(AddressAddNewDTO addressAddNewDTO) {
        log.debug("[添加收货地址]业务 参数:{}",addressAddNewDTO);
        //若设为默认地址则仅将当前地址设为默认
        if(addressAddNewDTO.getIsDefault() == 1){
            AddressStandardVO result = addressMapper.getByIsDefault();
            if(result != null){
                Address address = new Address();
                address.setIsDefault(0);
                address.setId(result.getId());
                addressMapper.updateById(address);
            }
        }
        //执行添加
        Address address = new Address();
        BeanUtils.copyProperties(addressAddNewDTO,address);
        address.setUserId(getUserId());
        int rows = addressMapper.insert(address);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void delete(Long[] ids) {
        log.debug("[删除收货地址]业务 参数个数:{}",ids.length);
        int rows = addressMapper.delete(ids);
        if(rows != ids.length){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public void updateById(AddressUpdateDTO addressUpdateDTO) {
        int count = addressMapper.countById(addressUpdateDTO.getId());
        if(count == 0){
            String message = "修改失败,访问的资源不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        //若设为默认地址则仅将当前地址设为默认
        if(addressUpdateDTO.getIsDefault() == 1){
            AddressStandardVO result = addressMapper.getByIsDefault();
            if(result != null){
                Address address = new Address();
                address.setIsDefault(0);
                address.setId(result.getId());
                addressMapper.updateById(address);
            }
        }
        //执行修改
        Address address = new Address();
        BeanUtils.copyProperties(addressUpdateDTO,address);
        int rows = addressMapper.updateById(address);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    @Override
    public AddressStandardVO getStandardById(Long id) {
        AddressStandardVO result = addressMapper.getStandardById(id);
        if(result == null){
            String message = "获取失败,访问的资源不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return result;
    }

    @Override
    public JsonPage<AddressListItemVO> list(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return JsonPage.restPage(addressMapper.list());
    }

    private LoginPrincipal getUserInfo(){
        UsernamePasswordAuthenticationToken authenticationToken=
                (UsernamePasswordAuthenticationToken)
                        SecurityContextHolder.getContext().getAuthentication();
        if(authenticationToken == null){
            throw new ServiceException(
                    ServiceCode.ERR_UNAUTHORIZED,"您没有登录!");
        }
        LoginPrincipal loginPrincipal = (LoginPrincipal)authenticationToken.getPrincipal();
        return loginPrincipal;
    }
    private Long getUserId(){
        return getUserInfo().getId();
    }

}
